<?php
/**
 * 产品属性管理 
 *
 * @copyright   Copyright 2011, Richie
 * @author      Richie <richbossman@163.com>
 * @package     SMS
 * @link        http://www.letphp.com
 * 
 * $Id$
 */
class CategoryAction extends SMSAction {
	
	function _before_index(){
		//$model = D('Category');
		//print_r($model->childrenId($model->getAllCategory(2)));
		//print_r(D('Attribute')->getAttribute());
	}
	
	function categoryList(){
		echo json_encode(D('Category')->getAllCategory());
	}
	
	function _before_add(){
		$this->global_assign();
	}
	
	function _before_edit(){
		$this->global_assign();
		
	}
	
	//供option使用的分类列表
	function optionCategory($data=array()){
		static $option = array();
		foreach($data as $cate){
			$cat = array(
				'id' => $cate['id'],
				'repeat' => str_repeat('&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;',$cate['level']-1),
				'name' => $cate['name']
			);
			$option[] = $cat;
			if(!empty($cate['children'])){
				$this->optionCategory($cate['children']);
			}
		}
		return $option;
	}
	
	//删除产品分类
	function deleteCategory(){
		$id = isset($_GET['id']) ? intval($_GET['id']) : 0;
		$product = M('product')->where(array('cat_id'=>$id))->count();
		if($product){
			$this->error('该分类下存在产品，暂时还不能删除该分类。');
		}
		$children = D('Category')->getAllCategory($id);
		if($children){
			$this->error('该分类存在子分类，暂时还不能删除该分类。');
		}
		//删除分类
		D('Category')->delete($id);
		$this->success('');
	}
	
	//公共赋值
	function global_assign(){
		$this->assign('catelist', $this->optionCategory(D('Category')->getAllCategory()));
		$this->assign('attribute', D('Attribute')->getAttribute());
	}
	
	/* 按钮 */
	
	function _button_category_index(){
		return array('add');
	}
	
	function _button_category_add(){
		return array('index');
	}
	
	function _button_category_edit(){
		return array('index', 'add');
	}
}
?>